<?php

namespace App\Http\Controllers\Home;

use App\Http\Requests\Login;
use App\Models\Configs;
use App\Models\IntegralLogs;
use App\Models\TemporaryStorages;
use App\Models\UserInfos;
use App\Models\Users;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;


class RegisterController extends Controller
{
    /**
     * 注册页面
     */
    public function register()
    {
        $title = '注册';
        return view('home.register.register',compact('title'));
    }

    /**
     * 供应商注册引导页面
     */
    public function registerGuide()
    {
        $title = '供应商注册引导';
        return view('home.register.guide',compact('title'));
    }

    /**
     * HR注册引导页面
     */
    public function hrRegisterGuide()
    {
        $title = 'HR注册引导';
        return view('home.register.hrguide',compact('title'));
    }

    /**
     * 供应商注册
     * @route home.register.supplierRegister
     *
     * @param mobile 手机号
     * @param password 密码
     * @param confirmc_pass 确认密码
     * @param code 短信验证码
     *
     * @return user_info 用户信息
     */
    public function supplierRegister(Login $request){
        $mobile = (string)$request->post('mobile');
        $password = (string)$request->post('password');
        $confirmc_pass = (string)$request->post('confirmc_pass');
        $code = (string)$request->post('code');
        if (empty($mobile) || empty($password) || empty($confirmc_pass) || empty($code)) return $this->writeJson(1,null,'参数错误');
        if(!preg_match('/^((13|14|15|16|17|18|19)[0-9]\d{8})$/',$mobile)) return $this->writeJson(1,null,'请填写正确的手机号码');
        if($password !== $confirmc_pass){
            return $this->writeJson(1,null,'两次密码输入不一致,请重新输入');
        }
//        //检查验证码是否正确
//        $cache = cache()->get('sms-'.$mobile);
//        if(empty($cache)) return $this->writeJson(1,null,'短信验证码已过期');//过期
//        if($cache['code'] != $code) return $this->writeJson(1,null,'短信验证码输入错误');
//        cache()->delete('sms-'.$mobile);//删除验证码
        //检测手机号是否被注册
        $check_user_mobile = Users::getUserInfo(['mobile' => $mobile]);
        if (!empty($check_user_mobile)) {
            return $this->writeJson(1,null,'该手机号已经注册');
        }

        $integral = Configs::getConfig('integral');//注册送积分
        $base = Configs::getConfig('base');
        $user_sn = 'HR'.substr(time(),-6);
        $now = date('Y-m-d H:i:s');
        $user_data = [
            'avatar' => $base['default_avatar'],
//            'user_sn' => $user_sn,
            'username' => $user_sn,
            'mobile' => $mobile,
            'password' => bcrypt($password),
            'integral' => $integral['bus_register'],
            'register_ip' => $request->ip(),
            'login_at' => $now,
            'login_ip' => $request->ip(),
            'type' => 2,
        ];
        //计算会员费有效期
        $days = $base['member_fee'];
        $user_data['member_fee_at'] = date('Y-m-d H:i:s',strtotime("$now + $days day"));

        $user_info = Users::create($user_data);
        $user_info['public_code'] = $base['public_code'];//公众号二维码
        //信息存入扩展表
        $data_info = [
            'user_id' => $user_info['id'],
        ];
        UserInfos::create($data_info);

        //添加日志记录
        $integral = [
            'user_id' => $user_info['id'],
            'user_name' => $user_info['mobile'],
            'integral' => '+'.$integral['bus_register'],
            'desc' => '注册送积分',
            'type' => 1,
        ];
        IntegralLogs::create($integral);
        return $this->writeJson(0,$user_info, '注册成功');
    }

    /**
     * HR注册注册第一步
     * @route home.register.hrStep1
     *
     * @param mobile 手机号
     * @param password 密码
     * @param confirmc_pass 确认密码
     */
    public function hrStep1(Login $request){
        $data = [
            'mobile' => (string)$request->post('mobile'),
            'password' => (string)$request->post('password'),
            'confirmc_pass' => (string)$request->post('confirmc_pass'),
            'created_at' => date('Y-m-d H:i:s'),
            'updated_at' => date('Y-m-d H:i:s'),
        ];
        if(!preg_match('/^((13|14|15|16|17|18|19)[0-9]\d{8})$/', $data['mobile']))   return $this->writeJson(1,'请填写正确的手机号码');
        if($data['password'] !== $data['confirmc_pass']){
            return $this->writeJson(1,null,'两次密码输入不一致,请重新输入');
        }

//        //检查验证码是否正确
//        $code = (string)$request->post('code');
//        if (empty($code)) return $this->writeJson(1,null,'请输入验证码');
//        $cache = cache()->get('sms-'.$data['mobile']);
//        if(empty($cache)) return $this->writeJson(1,null,'短信验证码已过期');//过期
//        if($cache['code'] != $code) return $this->writeJson(1,null,'短信验证码输入错误');
//        cache()->delete('sms-'.$data['mobile']);//删除验证码

        //检测手机号是否被注册
        $check_member_mobile = Users::getUserInfo(['mobile' => $data['mobile']]);
        if (!empty($check_member_mobile)) {
            return $this->writeJson(1,null,'该手机号已经注册');
        }
        unset($data['confirmc_pass']);
        $id = TemporaryStorages::insertGetId($data);
        return $this->writeJson(0,$id);
    }

    /**
     * HR注册第二步
     * @route home.register.hrStep2
     *
     * @param tid 暂存表id
     * @param username 姓名
     * @param email 邮箱
     * @param qq QQ
     * @param sex 姓名：1男 2女
     * @param company_name 公司名称
     * @param hr_department 部门
     * @param hr_position 职位
     * @param company_type 公司类型
     * @param company_industry 公司行业
     * @param company_size 公司规模
     */
    public function hrStep2(Request $request)
    {
        try {
            $tid = (int)$request->get('tid');//暂存表id
            $info = TemporaryStorages::where('id',$tid)->first();//暂存信息
            if (empty($info)) return $this->writeJson(1, null, '参数错误');

            $integral = Configs::getConfig('integral');//注册送积分
            $base = Configs::getConfig('base');//获取头像
            $data = [
                'avatar' => $base['default_avatar'],
//                'user_sn' => 'HR'.substr(time(),-6),
                'username' => (string)$request->post('username'),
                'nickname' => (string)$request->post('username'),
                'mobile' => $info['mobile'],
                'password' => bcrypt($info['password']),
                'email' => (string)$request->post('email'),
                'qq' => (string)$request->post('qq'),
                'sex' => (int)$request->post('sex'),
                'integral' => $integral['hr_register'],
                'register_ip' => $request->ip(),
                'login_ip' => $request->ip(),
                'login_at' => date('Y-m-d H:i:s'),
                'type' => 1,
            ];
            $user_info = Users::create($data);
            //信息存入扩展表
            $data_info = [
                'user_id' => $user_info['id'],
                'company_name' => (string)$request->post('company_name'),
                'company_type' => (int)$request->post('company_type'),
                'company_industry' => (int)$request->post('company_industry'),
                'company_size' => (int)$request->post('company_size'),
                'hr_department' => (string)$request->post('hr_department'),
                'hr_position' => (string)$request->post('hr_position'),
            ];
            UserInfos::create($data_info);
            //添加日志记录
            $integral = [
                'user_id' => $user_info['id'],
                'user_name' => $info['mobile'],
                'integral' => '+'.$integral['hr_register'],
                'desc' => '注册送积分',
                'type' => 1,
            ];
            IntegralLogs::create($integral);
            return $this->writeJson(0,$user_info, '新增用户信息成功');
        } catch (\Exception $e) {
            return $this->writeJson(1, null, $e->getMessage());
        }
    }

}